﻿var  tabsMap = new Map(); 
var tabIndex = 0;
var b;
$(document).ready(function () {
	 
		b=detectBrowser();
	 
		 $('#tabs').tabs({    
			    border:false,    
			    onSelect:function(title,index){    
			        // alert(tabs[index].id+' is selected');   
			    	tabIndex = index;
			    }    
			}); 
           $('.easyui-accordion li a').click(function () {
               var tabTitle = $(this).text();
               var id =  $(this).attr("id");
               var url = $(this).attr("href");
               if(url=='')
    		   {
        		   return false;
    		   }
               addTab(tabTitle, url,id);
               $('.easyui-accordion li div').removeClass("selected");
               $(this).parent().addClass("selected");
           }).hover(function () {
               $(this).parent().addClass("hover");
           }, function () {
               $(this).parent().removeClass("hover");
           });
 
           function addTab(subtitle, url,id) {
               if (!$('#tabs').tabs('exists', subtitle)) {
                   $('#tabs').tabs('add', {
                       title: subtitle,
                       id:"p_"+id,
                       content: createFrame(url,id),
                       closable: true,
                       onSelect:function(title){    
                           alert(title+' is selected');    
                       }, 
                       width: $('#mainPanle').width() - 10,
                       height: $('#mainPanle').height() - 26
                   });
               } else {
                   $('#tabs').tabs('select', subtitle);
              }
               tabClose();
           }
 
 
          
 
 
           function tabClose() {
               /*双击关闭TAB选项卡*/
               $(".tabs-inner").dblclick(function () {
                   var subtitle = $(this).children("span").text();
                   $('#tabs').tabs('close', subtitle);
               })
 
               $(".tabs-inner").bind('contextmenu', function (e) {
                   $('#mm').menu('show', {
                       left: e.pageX,
                       top: e.pageY
                   });
                   var subtitle = $(this).children("span").text();
                   $('#mm').data("currtab", subtitle);
                   return false;
               });
           }
 
           //绑定右键菜单事件
      function tabCloseEven() {
               //关闭当前
        $('#mm-tabclose').click(function () {
                   var currtab_title = $('#mm').data("currtab");
                   $('#tabs').tabs('close', currtab_title);
               })
               //全部关闭
        $('#mm-tabcloseall').click(function () {
                   $('.tabs-inner span').each(function (i, n) {
                       var t = $(n).text();
                      $('#tabs').tabs('close', t);
                   });
               });
 
               //关闭除当前之外的TAB
               $('#mm-tabcloseother').click(function () {
                   var currtab_title = $('#mm').data("currtab");
                   $('.tabs-inner span').each(function (i, n) {
                       var t = $(n).text();
                       if (t != currtab_title)
                           $('#tabs').tabs('close', t);
                   });
               });
               //关闭当前右侧的TAB
               $('#mm-tabcloseright').click(function () {
                   var nextall = $('.tabs-selected').nextAll();
                   if (nextall.length == 0) {
                      //msgShow('系统提示','后边没有啦~~','error');
                       alert('后边没有啦~~');
                       return false;
                   }
                   nextall.each(function (i, n) {
                       var t = $('a:eq(0) span', $(n)).text();
                       $('#tabs').tabs('close', t);
                   });
                   return false;
              });
               //关闭当前左侧的TAB
               $('#mm-tabcloseleft').click(function () {
                   var prevall = $('.tabs-selected').prevAll();
                   if (prevall.length == 0) {
                       alert('到头了，前边没有啦~~');
                       return false;
                   }
                   prevall.each(function (i, n) {
                       var t = $('a:eq(0) span', $(n)).text();
                       $('#tabs').tabs('close', t);
                   });
                   return false;
               });
 
               //退出
               $("#mm-exit").click(function () {
                   $('#mm').menu('hide');
 
               })
           }
       });
function createFrame(url,id) {
	   if(url=='')
	   {
		   return false;
	   }
    var s = '<iframe name="f_'+id+'" id="f_'+id+'" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
} 
//浮动粘贴板全局变量
 var clipArr = [];
 /**
  * 弹出提示信息
  * 
  * @param msg
  * @param times
  */
 function tips(msg, times) {
 	art.dialog.tips(msg, times);
 }
 function formateUrl(url) {
 	return ROOT_PATH + url;
 }
 /**
  * 锁屏 artdialog
  */
 function mask() {
	 $("#loadMsg").html('正在加载，请等待... ');
	 $("#loading").css({display:'block'});
//	 $.message.progress({
//		 title:"aaaaa"
//	 });
 }
 /**
  * 解锁artdialog
  */
 function unmask() {
	 $("#loading").css({display:'none'});
 }
 function maskPage() {
 	$("body").mask("正在处理,请稍候...");
 }
 function unmaskPage() {
 	$("body").unmask();
 }
 
 //更新消息内容
 function showMsg(msg)
 {
	 $("#loadMsg").html(msg+'&nbsp;&nbsp;&nbsp;&nbsp;');
 }
 function showTip(msg)
 {
 	$.messager.show({
 		title:'提示',
 		msg:msg,
 		showType:'show',
 		timeout:3000,
 		style:{
 			right:'',
 			top:document.body.scrollTop+document.documentElement.scrollTop,
 			bottom:''
 		}
 	});
 }
 var win;
 //调用激活tab页中的方法
 function refresh(fn)
 {
	 var pp = $('#tabs').tabs('getSelected');
	 var fid = "f_"+pp.panel('options').id.split("_")[1];
 	 var args = Array.prototype.slice.call(arguments,1);
 	 eval(fid).eval(fn).apply(this,args)
 }
 //弹出页面
 function showWin(url,options)	
{
	    var args = Array.prototype.slice.call(arguments,2);
		if(options.id)
		{
		}
		else
		{
			options.id = "windiv";
		}
		
		var d = document.getElementById(options.id);
		if(d)
		{}
		else
		{
			d = document.createElement("div");
			d.id = options.id;
			document.body.appendChild(d);
		}
		var f = createFrame(url,options.id);
		if(options.fn)
		{
			var win= $(d).dialog({ 
				  content:f,  
			   	  title:options.title,
			   	  id:options.id,
			   	  	width : options.w,
					height : options.h,
					loadingMessage:"加载数据",
				  collapsible:false,
				  minimizable:false,
				  maximizable:false,
				  modal:true
				 
				  ,buttons:[
				          {
				        	  text:"确定",iconCls:'icon-ok',handler:function(){
				        		  var fid = "f_"+options.id;
				        		  var o = document.getElementById(fid);
				        		  o.contentWindow.eval(options.fn).apply(this,args)
				        	  }
				          } 
				          ,{
				        	  text:"取消",iconCls:'icon-cancel',handler:function(){
				        		  $(d).window('close',true);
				            }
				          } 
				          ]
				}); 
		}
		else
		{
			var win= $(d).dialog({ 
				  content:f,  
			   	  title:options.title,
			   	  id:options.id,
			   	  	width : options.w,
					height : options.h,
					loadingMessage:"加载数据",
				  collapsible:false,
				  minimizable:false,
				  maximizable:false,
				  modal:true
				}); 
		}
		
		
		//alert(document.getElementById(id));
}
 
function closeWin(id)
{
	if(id)
	{
	}
	else
	{
		id = "windiv";
	}
	
	var d = document.getElementById(id);
	$(d).window('close',true);
}


function detectBrowser(){
	var browser = { 
		msie: false, firefox: false, opera: false, safari: false, 
		chrome: false, netscape: false, appname: 'unknown', version: 0 
	}, 
	userAgent = window.navigator.userAgent.toLowerCase(); 
	if (/(msie|firefox|opera|chrome|netscape)\D+(\d[\d.]*)/.test( userAgent )){
		browser[RegExp.$1] = true; 
		browser.appname = RegExp.$1; 
		browser.version = RegExp.$2; 
	} else if ( /version\D+(\d[\d.]*).*safari/.test( userAgent ) ){ // safari 
		browser.safari = true; 
		browser.appname = 'safari'; 
		browser.version = RegExp.$2; 
	} 
	return {"browser":browser.appname ,"version":browser.version}; 
} 